//
//  Api.m
//  TaurusXi
//
//  Created by xicheng on 15/12/8.
//  Copyright © 2015年 xicheng. All rights reserved.
//

#import "Api.h"
#import "NetworkHelper.h"
#import "MD5_Utils.h"
#import <AdSupport/AdSupport.h>
#import "UserHelper.h"


/**
 * baseurl
 */
 static const NSString  *BASEURL = @"http://wwwtest1.jnzuo.com/mobile/";
//static const NSString  *BASEURL = @"http://10.10.3.188:8080/mobile/";
//    public static String BASEURL = "http://10.10.3.135:8080/mobile/";

///**
// * 获取图片验证码
// *
// * @param timestamp 时间戳
// * @param uuid      时间戳的MD5用作当前请求的唯一表示,提交验证码的时候使用获取的时候的uuid
// */
//public static String getImageVerifyCode(long timestamp, String uuid) {
//  String url = BASEURL + "image/randcode.html?userId=" + Config.getUid(Config.currentContext) + "&t=" + (timestamp / 1000) + "&sign=" + Config.MD5(Config.getUid(Config.currentContext) + ((timestamp / 1000)) + NetworkUtils.appkey) + "&uuid=" + uuid;
//  return url;
//}
//
///**
// * 注册-提交验证码
// * params:
// * username 手机号
// * randCode 图片验证码
// * uuid 获取图片的UUID
// */
//public static final String register_first = BASEURL + "apiUser/register/setp/frist.html";
///**
// * 注册-提交验证码
// * params:
// * 	username: 用户名
// * 	smsCode: 短信校验码
// * 	inviteNo:邀请码
// * 	popularizeNo:渠道推广码
// * 	smsId: 短信id
// */
//public static final String register_second = BASEURL + "apiUser/register/setp/second.html";
///**
// * 注册-第三步
// * params:
// * 	realname:真实姓名
// * 	cardid:身份证号
// */
//public static final String register_third = BASEURL + "apiUser/register/setp/third.html";
///**
// * 注册-第三步
// * params:	bank:所属银行 英文简
// * 	cardNo:银行卡账号
// * 	phone:银行预留手机号
// * 	phoneCode:手机验证码
// * 	province:开户省份
// * 	city:开户城市
// * 	branch:支行
// */
//public static final String register_fourth = BASEURL + "apiUser/register/setp/fourth.html";
//
///**
// * 登录
// * params:
// * username
// * password
// */
//public static final String login = BASEURL + "apiUser/login.html";
///**
// * 获取短信
// * params
// * 	phone:手机号
// */
//public static final String getSms = BASEURL + "apiUser/getSms.html";
///**
// * 8.	找回密码第一步
// * parms:
// * 	username:手机号
// * 	randCode: 校验码
// * 	uuid: 校验码id
// */
//public static final String findpwd_first = BASEURL + "apiUser/findpwd/setp/frist.html";
///**
// * 8.	找回密码第二步
// * parms:
// * 	username:手机号
// * 	smsCode: 短信校验码
// * 	smsId: 短信id
// */
//public static final String findpwd_second = BASEURL + "apiUser/findpwd/setp/second.html";
///**
// * 8.	找回密码第三步
// * parms:
// * 	uuid: uuid
// * 	password: 密码
// * 	password2:密码2
// */
//public static final String findpwd_third = BASEURL + "apiUser/findpwd/setp/third.html";
///**
// * 8.	15.	设置密码
// * parms:
// * 	   password: password
// * 	   password2: password2
// * 	   payPassword: payPassword
// * 	   payPassword2: payPassword
// */
//public static final String setPassword = BASEURL + "apiUser/setPassword.html";
///**
// * 首页
// */
//public static final String index = BASEURL + "apiUser/index.html";
///**
// * 获取H5URL
// */
//public static final String getUrl = BASEURL + "apiCom/getUrl.html";

@implementation Api

+ (instancetype)shareApiInstance
{
  static Api *sharedInstance = nil;
  static dispatch_once_t onceToken = 0;
  dispatch_once(&onceToken, ^{
    sharedInstance = [self new];
  });
  return sharedInstance;
}


-(NSString *)getCodeStatusPath{
  //需要修改
  int currentTime = (int)[[NSDate date] timeIntervalSince1970];
  
  NSString *sign = [self getSignWithTimestamp:currentTime];
  self.codeUUID =[MD5_Utils  md5HexDigest:[NSString stringWithFormat:@"%d%@",currentTime,[self getUUID]]];
  NSString *url = [NSString stringWithFormat:@"%@image/randcode.html?userId=%@&t=%d&sign=%@&uuid=%@",BASEURL,[UserHelper shareUserInstance].userId, currentTime,sign,self.codeUUID];
  
  NSLog(@"url__%@",url);

  return url;
}


-(NSString *)getSignWithTimestamp:(int)timestamp{
  NSString *sign__ = [NSString stringWithFormat:@"%@%d%@",[UserHelper shareUserInstance].userId,timestamp,@"APPKEY2015JNZUO" ];
 return [MD5_Utils md5HexDigest:sign__];

}


/**
 *  获取唯一标示UUID
 */
- (NSString *)getUUID{
  //获取到的UUID
  return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}


- (void) testWithParams:(NSDictionary *)params AndResponce:(PassValueBlock)successBlock AndError:(PassErrorBlock)errorBlock{
  
  NetworkTask *task = [[NetworkTask alloc]init];
  task.httpURL = @"http://192.168.0.128:8888/test";
  task.tag = task.httpURL.copy;
  task.debugTag = task.httpURL.copy;
  task.params = params;
  task.shouldCacheFlag = NO;
  task.method = HttpMethodPost;
//  [task setDataResponce:successBlock andError:errorBlock];
  [[NetworkHelper sharedInstance]addTask:task WithSuccessBlock:successBlock AndErrorBlock:errorBlock];
}

-(void) registerFirstWithPhoneNumb:(NSString *)phoneNumb AndCode:(NSString *)code AndCodeUUID:(NSString *)codeUUID AndResponce:(PassValueBlock)successBlock AndError:(PassErrorBlock)errorBlock{
  NSDictionary *params = @{@"username":phoneNumb,@"randCode":code,@"uuid":codeUUID};
  
  NetworkTask *task = [[NetworkTask alloc]init];
  task.httpURL = [NSString stringWithFormat:@"%@%@",BASEURL,@"apiUser/register/setp/frist.html"];
  task.tag = task.httpURL.copy;
  task.debugTag = task.httpURL.copy;
  task.params = params;
  task.shouldCacheFlag = NO;
  task.method = HttpMethodPost;
   [[NetworkHelper sharedInstance]addTask:task WithSuccessBlock:successBlock AndErrorBlock:errorBlock];
}


//username: 用户名
//smsCode: 短信校验码
//inviteNo:邀请码
//popularizeNo:渠道推广码
//smsId: 短信id


-(void) registerSecondWithPhoneNumb:(NSString *)phoneNumb AndSMSCode:(NSString *)smsCode AndInviteNo:(NSString *)inviteNo AndPopularizeNo:(NSString *)popularizeNo AndSmsId:(NSString *)smsId AndResponce:(PassValueBlock)successBlock AndError:(PassErrorBlock)errorBlock{

  NSDictionary *params = @{@"username":phoneNumb,@"smsCode":smsCode,@"inviteNo":inviteNo,@"popularizeNo":popularizeNo,@"smsId":smsId};
  
  NetworkTask *task = [[NetworkTask alloc]init];
  task.httpURL = [NSString stringWithFormat:@"%@%@",BASEURL,@"apiUser/register/setp/second.html"];
  task.tag = task.httpURL.copy;
  task.debugTag = task.httpURL.copy;
  task.params = params;
  task.shouldCacheFlag = NO;
  task.method = HttpMethodPost;
  [[NetworkHelper sharedInstance]addTask:task WithSuccessBlock:successBlock AndErrorBlock:errorBlock];
  
  
}


-(void) loginWith:(NSString *)phoneNumb AndPassword:(NSString *)password AndResponce:(PassValueBlock)successBlock AndError:(PassErrorBlock)errorBlock{

  NSDictionary *params = @{@"username":phoneNumb,@"password":password};
  
  NetworkTask *task = [[NetworkTask alloc]init];
  task.httpURL = [NSString stringWithFormat:@"%@%@",BASEURL,@"apiUser/login.html"];
  task.tag = task.httpURL.copy;
  task.debugTag = task.httpURL.copy;
  task.params = params;
  task.shouldCacheFlag = NO;
  task.method = HttpMethodPost;
  [[NetworkHelper sharedInstance]addTask:task WithSuccessBlock:successBlock AndErrorBlock:errorBlock];

}


-(void) getIndexDataWithResponce:(PassValueBlock)successBlock AndError:(PassErrorBlock)errorBlock{
  NetworkTask *task = [[NetworkTask alloc]init];
  task.httpURL = [NSString stringWithFormat:@"%@%@",BASEURL,@"apiUser/index.html"];
  task.tag = task.httpURL.copy;
  task.debugTag = task.httpURL.copy;
  task.shouldCacheFlag = NO;
  task.method = HttpMethodPost;
  [[NetworkHelper sharedInstance]addTask:task WithSuccessBlock:successBlock AndErrorBlock:errorBlock];

}

-(void) getStarDataWithResponce:(PassValueBlock)successBlock AndError:(PassErrorBlock)errorBlock{
  NetworkTask *task = [[NetworkTask alloc]init];
  task.httpURL = [NSString stringWithFormat:@"%@%@",BASEURL,@"apiUser/myindex.html"];
  task.tag = task.httpURL.copy;
  task.debugTag = task.httpURL.copy;
  task.shouldCacheFlag = NO;
  task.method = HttpMethodPost;
  [[NetworkHelper sharedInstance]addTask:task WithSuccessBlock:successBlock AndErrorBlock:errorBlock];
}


-(void) gotoHtmlWith:(NSString *)key AndResponce:(PassValueBlock)successBlock AndError:(PassErrorBlock)errorBlock{
  NSDictionary *params = @{@"key":key};
  
  NetworkTask *task = [[NetworkTask alloc]init];
  task.httpURL = [NSString stringWithFormat:@"%@%@",BASEURL,@"apiCom/getUrl.html"];
  task.tag = task.httpURL.copy;
  task.debugTag = task.httpURL.copy;
  task.shouldCacheFlag = NO;
  task.params = params;
  task.method = HttpMethodPost;
  [[NetworkHelper sharedInstance]addTask:task WithSuccessBlock:successBlock AndErrorBlock:errorBlock];

}

-(NSString *)getHtmlUrlWith:(NSString *)key{
 int currentTime = (int)[[NSDate date] timeIntervalSince1970];
  NSString *userId = [[UserHelper shareUserInstance] userId];
  return [NSString stringWithFormat:@"%@%@&userId=%@&t=%d&sign=%@&from=h5",BASEURL,key,userId,currentTime,[self getSignWithTimestamp:currentTime]];
}


@end
